package dao

import (
	"ginFlutterBolg/internal/model"
	"ginFlutterBolg/internal/util"
)

type (
	UserDao struct {
	}
)

// Insert 插入用户
func (u *UserDao) Insert(user *model.User) error {
	orm := util.Orm
	_, err := orm.InsertOne(user)
	return err
}

// LoginByUsername 通过用户名登录
func (u *UserDao) LoginByUsername(user *model.User) (bool, error) {
	var res bool
	orm := util.Orm
	res, err := orm.Get(user)
	return res, err
}

// LoginByAccount 用户登录通过账户
func (u *UserDao) LoginByAccount(user *model.User) (bool, error) {
	var res bool
	orm := util.Orm
	res, err := orm.Get(user)
	return res, err
}

// GetUserByUid 获取用户id
func (u *UserDao) GetUserByUid(user *model.User) error {
	orm := util.Orm
	_, err := orm.Get(user)
	return err
}

// QueryBySecret 通过密钥查询
func (u *UserDao) QueryBySecret(user *model.User) (bool, error) {
	orm := util.Orm
	res, err := orm.Where("username = ?", user.Username).Get(user)

	return res, err
}

// ReName 重命名
func (u *UserDao) ReName(user *model.User) error {
	orm := util.Orm
	_, err := orm.Where("id = ?", user.Id).Update(user)
	if err != nil {
		return err
	}
	return nil
}

// SearchByContainUsername 包含用户名搜索
func (u *UserDao) SearchByContainUsername(page int, name string) (*[]model.User, error) {
	orm := util.Orm
	userList := make([]model.User, 0)
	err := orm.
		Where("username LIKE ?", "%"+name+"%").
		Limit(10, page*10).
		OrderBy("create_time").
		Find(&userList)
	if err != nil {
		return &userList, err
	}
	return &userList, err

}
